package com.app.service.impl;

import com.app.mapper.jiuquan.CommentMapper;
import com.app.mapper.jiuquan.UserMapper;
import com.app.model.CommentModel;
import com.app.model.JiuquanUserModel;
import com.app.service.CommentService;
import com.app.util.StringTool;
import com.app.vo.CommentVo;
import com.app.vo.IdCountVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

//评论
@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, CommentModel> implements CommentService {

    @Autowired
    private CommentMapper commentMapper;

    @Autowired
    private UserMapper userMapper;

    @Override
    public CommentVo getCommentByTypeList(Integer page, Integer limit, Integer type, Integer relationId, Integer commentType) {
        Page<CommentModel> page1 = new Page<>(page, limit);
        QueryWrapper<CommentModel> qw = new QueryWrapper<>();

        qw.eq("type", type);
        qw.eq("relation_id", relationId);
        qw.eq("check_status", 1);

        if (commentType == 2 || commentType == 3 || commentType == 4) {
            qw.eq("comment_type", commentType);
        }
        qw.orderByDesc("create_time");
        Page<CommentModel> commentPage = commentMapper.selectPage(page1, qw);
        List<CommentModel> commentList = commentPage.getRecords();
        for (CommentModel comment : commentList) {
            JiuquanUserModel user = userMapper.getByOpenid(comment.getOpenId());
            comment.setUserName(user.getRealName());
            comment.setFaveUrl(user.getFaceUrl());
            //分数转文字
            Integer commentTypeInt = comment.getCommentType();
            if (commentTypeInt == 2) {
                comment.setScoreText("好评");//好评
            }
            if (commentTypeInt == 3) {
                comment.setScoreText("中评");//中评
            }
            if (commentTypeInt == 4) {
                comment.setScoreText("差评");//差评
            }
            comment.setOpenId(null);
        }
        long count = commentPage.getTotal();
        CommentVo commentVo = new CommentVo();
        commentVo.setList(commentList);
        commentVo.setCount((int) count);

        List<IdCountVo> idCountVoList = new ArrayList<>();
        IdCountVo vo1 = new IdCountVo();
        vo1.setId(1);
        vo1.setName("最新");
        idCountVoList.add(vo1);
        for (int i = 2; i <= 4; i++) {
            IdCountVo vo = new IdCountVo();
            QueryWrapper<CommentModel> queryWrapperType = new QueryWrapper<>();
            queryWrapperType.eq("type", type);
            queryWrapperType.eq("relation_id", relationId);
            queryWrapperType.eq("check_status", 1);
            queryWrapperType.eq("comment_type", i);
            Integer typeCount = commentMapper.selectCount(queryWrapperType);
            vo.setId(i);
            if (i == 2) {
                vo.setName("好评");
                vo.setCount(typeCount);
            }
            if (i == 3) {
                vo.setName("中评");
                vo.setCount(typeCount);
            }
            if (i == 4) {
                vo.setName("差评");
                vo.setCount(typeCount);
            }
            idCountVoList.add(vo);
        }
        commentVo.setCommentTypeList(idCountVoList);
        return commentVo;
    }

    @Override
    public CommentVo getCommentList(Integer page, Integer limit, String startTime, String endTime, Integer checkStatus, Integer commentType) {
        Page<CommentModel> page1 = new Page<>(page, limit);
        QueryWrapper<CommentModel> qw = new QueryWrapper<>();
        if (StringTool.isNotEmpty(startTime) && StringTool.isNotEmpty(endTime)) {
            qw.ge("create_time", startTime);
            qw.le("create_time", endTime);
        }
        if (checkStatus != null) {
            qw.eq("check_status", checkStatus);
        }

        if (commentType != null) {
            qw.eq("comment_type", commentType);
        }
        qw.orderByDesc("create_time");
        Page<CommentModel> commentPage = commentMapper.selectPage(page1, qw);

        List<CommentModel> commentList = commentPage.getRecords();
        for (CommentModel comment : commentList) {
            //审核转文字
            Integer check = comment.getCheckStatus();
            if (check == 0) {
                comment.setCheckStatusText("待审核");
            }
            if (check == 1) {
                comment.setCheckStatusText("已审核");
            }
            //类型转文字
            Integer type = comment.getType();
            if (type == 1) {
                comment.setTypeText("场地");
            }
            if (type == 2) {
                comment.setTypeText("导师");
            }
            //分数转文字
            Integer commentTypeInt = comment.getCommentType();
            if (commentTypeInt == 2) {
                comment.setScoreText("好评");//好评
            }
            if (commentTypeInt == 3) {
                comment.setScoreText("中评");//中评
            }
            if (commentTypeInt == 4) {
                comment.setScoreText("差评");//差评
            }
            comment.setOpenId(null);
        }
        long count = commentPage.getTotal();
        CommentVo commentVo = new CommentVo();
        commentVo.setList(commentList);
        commentVo.setCount((int) count);
        return commentVo;
    }
}
